package svc

import (
	"github.com/bwmarrin/snowflake"
	_ "github.com/lib/pq"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"gorm.io/gorm"
	"xialou_platform/services/agent/model"
	"xialou_platform/services/agent/rpc/internal/config"
)

type ServiceContext struct {
	Config         config.Config
	Flake          *snowflake.Node
	DB             *gorm.DB
	AgentModel     model.AgentModel
	CourseModel    model.CourseModel
	StoreModel     model.StoreModel
	StoreLogModel  model.StoreLogModel
	StoreTypeModel model.StoreTypeModel
	AgentUserModel model.AgentUserModel
}

func NewServiceContext(c config.Config) *ServiceContext {
	conn := sqlx.NewSqlConn("postgres", c.Postgres.DataSource)
	flake, err := snowflake.NewNode(c.SnowflakeNode)
	if err != nil {
		panic(err)
	}
	return &ServiceContext{
		Config:         c,
		Flake:          flake,
		AgentModel:     model.NewAgentModel(conn, c.CacheRedis),
		CourseModel:    model.NewCourseModel(conn, c.CacheRedis),
		StoreModel:     model.NewStoreModel(conn, c.CacheRedis),
		StoreLogModel:  model.NewStoreLogModel(conn, c.CacheRedis),
		StoreTypeModel: model.NewStoreTypeModel(conn, c.CacheRedis),
		AgentUserModel: model.NewAgentUserModel(conn, c.CacheRedis),
	}
}
